package cn.wanda.bi.sunshine.platform.server.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@MapperScan(basePackages = "cn.wanda.bi.sunshine.platform.server.bean.mapper", sqlSessionFactoryRef="biSunshineSqlSessionFactory")
public class BiSunshineDataSourceConfig {
	
	@Value("${spring.datasource.driver-class-name}")
	private String driverClassName;
	@Value("${spring.datasource.url}")
	private String url;
	@Value("${spring.datasource.username}")
	private String username;
	@Value("${spring.datasource.password}")
	private String password;
	
	@Bean(name = "biSunshineDataSource")
	@Primary
	public DataSource biSunshineDataSource() {
		DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);  
        dataSource.setUrl(url);  
        dataSource.setUsername(username);  
        dataSource.setPassword(password);
        dataSource.setDbType("com.alibaba.druid.pool.DruidDataSource");
        return dataSource;
	}
	
	@Bean(name = "biSunshineTransactionManager")
	@Primary
    public DataSourceTransactionManager biSunshineTransactionManager() {
      return new DataSourceTransactionManager(biSunshineDataSource());
    }
	
    @Bean(name = "biSunshineSqlSessionFactory")
    @Primary
    public SqlSessionFactory biSunshineSqlSessionFactory() throws Exception {
      SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
      sessionFactory.setDataSource(biSunshineDataSource());
      return sessionFactory.getObject();
    }
 
}
